package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.oscar.ehome.R;
import com.oscar.ehome.base.fragment.BaseFragment;
import com.oscar.ehome.bean.pagebean.ClassRoomBean;
import com.oscar.ehome.bean.pagebean.ClassRoomPrombleBean;
import com.oscar.ehome.presenter.ClassRoomPresenter;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshBase;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshListView;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.adapter.ClassRoomPrombleAdapter;
import com.oscar.ehome.view.pageview.view.ClassRoomView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/9/19
 * 小课堂问答页
 */

public class ClassRoomPrombleFragment extends BaseFragment<ClassRoomView, ClassRoomPresenter> implements ClassRoomView,
        PullToRefreshBase.OnRefreshListener, ProgressFrameLayout.AgainRequestListener {

    @BindView(R.id.class_promble_list)
    PullToRefreshListView classPrombleList;

    @BindView(R.id.claaroompromble_fragment)
    ProgressFrameLayout claaroomprombleFragment;

    private View view;

    ClassRoomPresenter presenter;
    ClassRoomPrombleAdapter adapter;
    List<ClassRoomPrombleBean.PrombleListBean> list = new ArrayList<>();
    private String token = "";
    String knowledgeId = "";

    String messagebean = "";
    ClassRoomBean.ClassRoomListBean messbean;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_classroomproble, null);
        ButterKnife.bind(this, view);
        initView();
        onClick();
        initData();
        return view;
    }

    @Override
    protected ClassRoomPresenter createPresenter() {
        presenter = new ClassRoomPresenter(this);
        return presenter;
    }

    /**
     * 初始数据
     */
    public void initView() {
        messagebean = getActivity().getIntent().getStringExtra("messagebean");
        if (messagebean != null && !"".equals(messagebean)) {
            messbean = new Gson().fromJson(messagebean, ClassRoomBean.ClassRoomListBean.class);
        }
        if (messbean != null) {
            if (messbean.getId() != null) {
                knowledgeId = messbean.getId();
            }
        }

        token = SystemConfigFactory.getInstance(getActivity()).getSystemConfig().getToken();

        adapter = new ClassRoomPrombleAdapter(getActivity(), list);
        classPrombleList.setAdapter(adapter);
    }

    /**
     * 请求列表
     */
    public void initData() {
        Log.d("mwh", "knowledgeId" + knowledgeId);
        presenter.getPrombleList(token, knowledgeId);
        showLoading();
    }

    public void onClick() {
        classPrombleList.setPullLoadEnabled(true);
        classPrombleList.setOnRefreshListener(this);
        claaroomprombleFragment.setAgainRequestListener(this);
    }


    @Override
    public void knowledgeList(ClassRoomBean result) {

    }

    @Override
    public void problemList(ClassRoomPrombleBean result) {
        dismissLoading();
        classPrombleList.onPullDownRefreshComplete();
        classPrombleList.onPullUpRefreshComplete();
        if (result != null) {
            if (result.getList() != null) {
                list = new ArrayList<>();
                list = result.getList();
                adapter.setLists(list);
            }
        } else {
            classPrombleList.setVisibility(View.GONE);
            claaroomprombleFragment.setNoResult();
        }
    }

    @Override
    public void sendpromble(String result) {

    }

    @Override
    public void erroy() {
        dismissLoading();
        ToastUtil.show(getActivity(), getString(R.string.erry));
        classPrombleList.onPullDownRefreshComplete();
        classPrombleList.onPullUpRefreshComplete();
        claaroomprombleFragment.setLoadFail();
        classPrombleList.setVisibility(View.GONE);
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        ToastUtil.show(getActivity(), result);
        classPrombleList.onPullDownRefreshComplete();
        classPrombleList.onPullUpRefreshComplete();
        if (result.contains("过期")) {
            getActivity().getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(getActivity()).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(getActivity()).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
            getActivity().finish();
        } else {
            claaroomprombleFragment.setLoadFail();
            classPrombleList.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        initData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        initData();
    }

    @Override
    public void againRequest() {
        initData();
    }
}



